package com.ruoyi.framework.web.service;


import com.ruoyi.common.constant.DeviceConstants;
import com.ruoyi.common.constant.LoginConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.AndroidDevice;
import com.ruoyi.system.domain.YxDeviceLocation;
import com.ruoyi.system.domain.YxUserAndDevice;
import com.ruoyi.system.service.*;
import com.ruoyi.system.service.impl.SysUserServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * 位置服务业务类
 */
@Component
public class LocationService {

    private static final Logger log = LoggerFactory.getLogger(SysUserServiceImpl.class);


    @Autowired
    private RedisCache redisUtil;

    @Autowired
    private IYxUserAndDeviceService yxUserAndDeviceService;

    @Autowired
    private IYxDeviceAndGetuiService yxDeviceAndGetuiService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private IAndroidDeviceService androidDeviceService;

    @Autowired
    private IYxDeviceLocationService deviceLocationService;

    public R<String> uploadLocation(YxDeviceLocation yxDeviceLocation) {

        //获取当前登录用户
        SysUser user = SecurityUtils.getLoginUser().getUser();

        log.debug( "bindCid  userid ：" + user.getUserId());
        SysUser indb = userService.selectUserById(user.getUserId());
        if(indb == null || indb.getAccountType() != LoginConstants.USER_TYPE_DEVICE_STU){
            return R.fail("设备信息异常");
        }

        AndroidDevice query = new AndroidDevice();
        query.setDeviceId(indb.getUserName());
        List<AndroidDevice> androidDeviceList = androidDeviceService.selectAndroidDeviceList(query);
        if(androidDeviceList == null || androidDeviceList.size() != 1){
            return R.fail("设备信息异常");
        }

        YxDeviceLocation insert = new YxDeviceLocation();
        insert.setAddress(yxDeviceLocation.getAddress());
        insert.setLatitude(yxDeviceLocation.getLatitude());
        insert.setLongitude(yxDeviceLocation.getLongitude());
        insert.setDeviceId(androidDeviceList.get(0).getDeviceId());
        insert.setCreateTime(new Date());

        deviceLocationService.insertYxDeviceLocation(insert);


        return R.ok("上报成功");
    }

    public R<YxDeviceLocation> getDeviceLocation() {

        //获取当前登录用户
        SysUser user = SecurityUtils.getLoginUser().getUser();

        //获取当前默认绑定设备
        YxUserAndDevice query = new YxUserAndDevice();
        query.setUserId(user.getUserId());
        query.setIsDefult(DeviceConstants.USER_DEFULT_DEVICE);
        List<YxUserAndDevice> yxUserAndDeviceList= yxUserAndDeviceService.selectYxUserAndDeviceList(query);
        if(yxUserAndDeviceList == null || yxUserAndDeviceList.size() != 1){
            return R.fail("当前用户暂无绑定设备");
        }

        YxDeviceLocation yxDeviceLocation = deviceLocationService.selectYxDeviceLocationLastOne(yxUserAndDeviceList.get(0).getDeviceId());

        // 查询数据
        return R.ok(yxDeviceLocation);

    }



}
